package org.bees.cms.web.util.velocity.renderer;

import java.io.Serializable;

import net.sf.json.JSONObject;

import org.bees.cms.web.util.Beans;
import org.bees.cms.web.util.velocity.element.Image;

public class ImageRenderer implements Renderer {
	private Image image = new Image();

	@Override
	public void initialize(JSONObject json) throws Exception {
		image.from(json);
	}

	@Override
	public String render(Serializable t) throws Exception {
		String value = Beans.getString(t, image.getProperty());
		String src = image.getContext() + image.getAction() + (image.getAction().equals("") ? "" : image.getAction().contains("?") ? "&" : "?") + image.getName() + (image.getName().equals("") ? "" : "=") + value;
		image.setSrc(src);
		return image.toHTML();
	}

	@Override
	public void destroy() throws Exception {
		image = null;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

}
